package com.msmall.community.common.util;

public final class ContentUtil {

	private static final char QUOTE = '"';
	private static final char NBSP = ' ';
    private static final char AMP = '&';
    private static final char LT = '<';
    private static final char GT = '>';
    private static final char L_IP = '[';
    private static final char R_IP = ']';
    private static final String P_END = "</p>";
    private static final String SPAN_END = "</span>";
    private static final String EM_BEGIN = "<em>";
    private static final String EM_END = "</em>";
    private static final String U_BEGIN = "<u>";
    private static final String U_END = "</u>";
    private static final String STRONG_BEGIN = "<strong>";
    private static final String STRONG_END = "</strong>";
    private static final String BR = "<br/>";
    private static final String HTML_SPACE = "&nbsp;";
	private static final String WML_SPACE = "&#160;";
	
    private static final char[] QUOTE_ENCODE = "&quot;".toCharArray();
    private static final char[] AMP_ENCODE = "&amp;".toCharArray();
    private static final char[] LT_ENCODE = "&lt;".toCharArray();
    private static final char[] GT_ENCODE = "&gt;".toCharArray();
    
    private static final String BR_TAG_REGEX = "^<br( *)/>";
    private static final String BR_MULTI_TAG_REGEX = "(<br( *)/>)+";
    
//    private static final String WML_IMG_TAG_REGEX = "^<img( *)([^<^>]*)/>";
    private static final String IMG_TAG_REGEX = "^<img( +)src=\"(((?!\").)*)\"( *)/>";
    
    private static final String IMG_TAG_REGEX_ALT = "^<img( +)src=\"(((?!\").)*)\"( +)alt=\"(((?!\").)*)\"( *)/>";
    
    private static final String PLATE_CODE_REGEX = "^\\[img([1-9]{1}[0-9]*)\\]";
    
    private static final String A_TAG_REGEX = "^<a( +)href=\"(((?!\").)*)\">(((?!</a>).)*)</a>";
    
    private static final String P_TAG_BEGIN_REGEX = "<p[^>]*>"; //带样式的P标签
    private static final String SPAN_TAG_BEGIN_REGEX = "<span[^>]*>";//带样式的SPAN标签
    private static final String INPUT_IMG_TAG_BEGIN_REGEX = "<input([^>]*)type=\"image\"[^>]*src=\"([^>^\"]*)\"[^>]*/>";//IMG标签的INPUT表示
    private static final String IMG_ALT_REGEX = "alt=\"[^>^\"]*\"";
    private static final String FONT_BEGIN_REGEX = "<font[^>]*>"; //font样式
    private static final String FONT_END_REGEX = "</font>"; //font
    
	/**
     * 处理内容，使内容中的html标签/CSS全部删除
     */
    public static String escapeHtmlTags(String str){
    	str = str.replaceAll(P_TAG_BEGIN_REGEX, "") //<p>标签的开始标签，全部删除
        .replaceAll(P_END, "") //<p>标签的结束标签，全部替换成换行
        .replaceAll(EM_BEGIN, "")//<em>标签的开始标签，全部删除
        .replaceAll(EM_END, "")//<em>标签的结束标签，全部删除
        .replaceAll(U_BEGIN, "")//<em>标签的开始标签，全部删除
        .replaceAll(U_END, "")//<em>标签的结束标签，全部删除
        .replaceAll(STRONG_BEGIN, "")//<strong>标签的开始标签，全部删除
        .replaceAll(STRONG_END, "")//<strong>标签的结束标签，全部删除
        .replaceAll(SPAN_TAG_BEGIN_REGEX, "")//<span>标签的开始标签，全部删除
        .replaceAll(SPAN_END, "")//<span>标签的结束标签，全部删除
        .replaceAll(IMG_ALT_REGEX, "") //删除所有IMG标签的ALT
    	.replaceAll(HTML_SPACE, " ") //替换所有&nbsp;成空格
    	.replaceAll("&gt;", ">")
    	.replaceAll("&lt;", "<")
    	.replaceAll("&amp;", "&")
    	.replaceAll("&quot;", "\"")
    	.replaceAll("\\$\\$", "\\$")
    	.replaceAll("&ldquo;", "“")
    	.replaceAll("&rdquo;", "”")
    	.replaceAll("&rsquo;", "’")
    	.replaceAll("&lsquo;", "‘").trim();
    	
    	return str;
    }
}
